﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MuaChung.Base;
using MuaChung.Models;


namespace MuaChung
{
    /// <summary>
    /// Summary description for MuaChung5B
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class MuaChung5B : System.Web.Services.WebService
    {
        [WebMethod]
        public string Authenticate(string username, string password)
        {
            string sid = BaseWeb.GenerateRandomCode(10);
            if (AccountModel.ValidateUser(username, password))
            {
                HttpContext.Current.Cache.Add("sid", sid,null,DateTime.MaxValue,TimeSpan.FromMinutes(10),System.Web.Caching.CacheItemPriority.Default,null);
                return sid;
            }
            else
            {
                return null;
            }
        }

        [WebMethod]
        public bool ConfirmCoupon(string sid, string cid, float price, string unit)
        {            
            if (HttpContext.Current.Cache.Get("sid").ToString() != sid)
            {
                return false;
            }
            List<DONHANG> lstDH = DonHangModels.FindAllDonHangByMaCoupon(cid);
            foreach (var item in lstDH)
            {
                item.Gia = Convert.ToDecimal(price);
                item.TongGia = Convert.ToDecimal(price * item.SoLuong);
                item.TrangThai = 3;
                DonHangModels.Update(item);
                DonHangModels.SendEmailToConfirmOrder(item.ID);                
            }
            return true;
        }
    }
}
